﻿@model Proggy.Data.Entities.RandomDataItem
@{
    ViewBag.Title = (ViewBag.IsCreate ? "Create new item" : "Edit \"" + Model.RandomDataItemId + "\"");
    var buttonText = (ViewBag.IsCreate ? "Create" : "Save");
}
<h2>@ViewBag.Title</h2>
<div class="proggy-form-div rnd-editcreate">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false) 
        <h4>@Html.LabelFor(m => m.RandomDataItemId)</h4>
        <p>
            @if (ViewBag.IsCreate)
            {
                @:@Html.TextBoxFor(m => m.RandomDataItemId, new { @class = "proggy-textbox proggy-textbox-wide" })            
                @:&nbsp;@Html.ActionLink("<< get a random id", "GetUniqueId", null, new { id = "rnd_getId", target = "_blank" })
            }
            else { 
                <span class="italic red">@Model.RandomDataItemId</span>
                @Html.HiddenFor(m => m.RandomDataItemId)
            }
        </p>
        <h4>@Html.LabelFor(m => m.CategoryAlias)</h4>
        <p>@Html.TextBoxFor(m => m.CategoryAlias, new { @class = "proggy-textbox proggy-textbox-wide" })</p>
        <h4>@Html.LabelFor(m => m.Value)</h4>
        <div>
            @Html.TextAreaFor(m => m.Value, new { @class = "proggy-textarea proggy-textarea-large" })
        </div>
        <p>
            <input type="submit" class="proggy-button-go" value="@buttonText" />
            @Html.ActionLink("Cancel", "Index", null, new { success = 5, categoryAlias = Model.CategoryAlias }, new { @class = "proggy-button-stop" })
        </p>
    }
</div>
<script type="text/javascript">

    // onload
    jQuery(function () {

        // get link 
        var link = jQuery("a#rnd_getId");

        // get text box
        var textbox = jQuery("input#RandomDataItemId");

        // bind
        link.click(function (e) {

            //
            e.preventDefault();

            // get url
            var url = link.attr("href");

            // hide
            //link.hide();

            // post
            jQuery.get(url, function (r, s, x) {
                textbox.val(r);
            });

        });

        // get cats textbox
        var catTb = jQuery("input#CategoryAlias");

        // get data
        var catData = jQuery.parseJSON('@Html.Raw(ViewBag.CategoriesJson.Replace("'", "\\'"))');
       
        // set
        catTb.autocomplete({
            source: catData,
            delay: 0,
            minLength: 1
        });
    });
</script>
